<?php

namespace App\Admin\Metrics\Examples;

use App\Admin\Controllers\AdminController;
use App\Enums\DataPermission;
use App\Helpers\KeyHandler;
use App\Models\ReturnPerformance;
use App\Services\AdminService;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;

class MerchantPerformance extends Line
{
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title('复诊部成交额');
        $this->dropdown([
            '5' => '过去5天',
            '10' => '过去10天',
            '15' => '过去15天',
            '30' => '过去30天',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $adminController = app(AdminController::class);
        $data_permission = $adminController->getAdminDataPermission();
        $user_id = $adminController->getAdminUserId();
        $days = $request->get('option', 5);
        $key = $data_permission == DataPermission::ALL ? sprintf(KeyHandler::HOME_MERCHANT_PERFORMANCE, $days) : sprintf(KeyHandler::HOME_MERCHANT_PERSONAL_PERFORMANCE, $days, $user_id);
        $data = AdminService::adminHomePersonalData(new ReturnPerformance(), $key, $days, $data_permission, $user_id);
        $this->withContent($data['total'] ?? 0);
        // 图表数据
        $this->withChart($data['list']);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [
                [
                    'name' => $this->title,
                    'data' => $data,
                ],
            ],
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     * @return Sale.\App\Admin\Metrics\Examples\Sale.content
     */
    public function withContent(string $content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }
}
